package com.ruoyi.business.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.business.domain.AcContractInfo;
import com.ruoyi.business.mapper.AcContractInfoMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.websocket.WebSocketUsers;
import com.ruoyi.system.domain.AcMessage;
import com.ruoyi.system.domain.AcWorkOrder;
import com.ruoyi.system.mapper.AcMessageMapper;
import com.ruoyi.system.mapper.AcWorkOrderMapper;
import com.ruoyi.system.service.IAcMessageService;
import com.ruoyi.system.service.IAcWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时任务调度-预警管理测试
 *
 */
@Component("warntask")
public class WarnTask
{

    @Autowired
    private IAcWorkOrderService acWorkOrderService;
    @Autowired
    private AcWorkOrderMapper acWorkOrderMapper;



    @Autowired
    private IAcMessageService acMessageService;
    @Autowired
    private AcMessageMapper acMessageMapper;

    @Autowired
    private AcContractInfoMapper acContractInfoMapper;


    /**
     * 定时任务预警管理
     */
    public AjaxResult warnTask(){
        AcMessage acMessage = new AcMessage();
        acMessage.setIsRead("0");
        List<AcMessage> list =   acMessageMapper.selectAcMessageList(acMessage);
        WebSocketUsers.sendMessageToUsersByText(JSON.toJSONString(list));
        return AjaxResult.success(list);
    }
/**
 *
 * 合同到期提醒
 * **/
    public AjaxResult warnContractTask(){
        AcMessage acMessage = new AcMessage();
        acMessage.setIsRead("0");
        //合同10天内到期查询
        List<AcContractInfo> result =acContractInfoMapper.selectAcContractInfoByExpireDate();
        for (AcContractInfo acContractInfo : result) {
            acMessage.setContent("合同编号："+acContractInfo.getContractCode()+"，合同名称："+acContractInfo.getContractAssetName()+"，合同到期时间："+acContractInfo.getContractEndDate());
            acMessage.setMessageType("合同到期提醒");
            acMessage.setReservedField(acContractInfo.getContractCode());
        // 查询message表是否有该条数据
            AcMessage acMessageDev = new AcMessage();
            acMessageDev.setReservedField(acContractInfo.getContractCode());
            List<AcMessage>  acMessage1 = acMessageMapper.selectAcMessageList(acMessageDev);
            if (acMessage1.size()>0){
                acMessageMapper.updateAcMessage(acMessage);
            }else{
                acMessageService.insertAcMessage(acMessage);
            }
        }
        List<AcMessage> list =   acMessageMapper.selectAcMessageList(acMessage);
        WebSocketUsers.sendMessageToUsersByText(JSON.toJSONString(list));
        return AjaxResult.success(list);
    }

}
